package com.bysj.sausys.service.impl;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.beans.Pagination;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.dao.SysAssCadreMapper;
import com.bysj.sausys.exception.ParamException;
import com.bysj.sausys.model.SysAssCadre;
import com.bysj.sausys.service.ISysAssCadreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社团干部Service
 *
 * @author WangZK
 */
@Service("sysAssCadreService")
public class SysAssCadreServiceImpl implements ISysAssCadreService {

    @Resource
    private SysAssCadreMapper sysAssCadreMapper;

    /**
     * 保存社团干部信息
     *
     * @param sysAssCadre
     * @param map
     */
    @Override
    public void save(SysAssCadre sysAssCadre, Map map) {
        //进行数据校验
        if (!UserConst.getCurrentUserCategory(map).equals(UserConst.SHETUANGUANLIYUAN)) {
            throw new ParamException("用户类型有误");
        }
        if (checkAssCadreExist(sysAssCadre.getId(), sysAssCadre.getStuNum(), UserConst.getCurrentAssId(map))) {
            throw new ParamException("学号已存在，保存失败");
        }

        sysAssCadre.setAssId(UserConst.getCurrentAssId(map));
        sysAssCadre.setInsertMan(UserConst.getCurrentUsername(map));
        sysAssCadre.setInsertTime(new Date());
        sysAssCadreMapper.insertSelective(sysAssCadre);
    }

    /**
     * 判断社团干部是否已存在
     *
     * @param id
     * @param stuNum
     * @param assId
     * @return
     */
    private boolean checkAssCadreExist(Integer id, String stuNum, Integer assId) {
        return sysAssCadreMapper.countByStuNumAndAssId(id, stuNum, assId) > 0;
    }

    /**
     * 更新社联干部信息
     *
     * @param sysAssCadre
     * @param map
     */
    @Override
    public void update(SysAssCadre sysAssCadre, Map map) {
        //进行数据校验
        if (!UserConst.getCurrentUserCategory(map).equals(UserConst.SHETUANGUANLIYUAN)) {
            throw new ParamException("用户类型有误");
        }
        SysAssCadre before = sysAssCadreMapper.selectByPrimaryKey(sysAssCadre.getId());
        if (before == null) {
            throw new ParamException("待更新的干部信息不存在");
        }
        if (checkAssCadreExist(sysAssCadre.getId(), sysAssCadre.getStuNum(), UserConst.getCurrentAssId(map))) {
            throw new ParamException("学号已存在，更新失败");
        }

        SysAssCadre after = new SysAssCadre();

        after.setId(sysAssCadre.getId());
        after.setStuNum(sysAssCadre.getStuNum());
        after.setName(sysAssCadre.getName());
        after.setSex(sysAssCadre.getSex());
        after.setAnnual(sysAssCadre.getAnnual());
        after.setCollege(sysAssCadre.getCollege());
        after.setMajor(sysAssCadre.getMajor());
        after.setAssId(UserConst.getCurrentAssId(map));
        after.setDept(sysAssCadre.getDept());
        after.setPosition(sysAssCadre.getPosition());
        after.setSanction(sysAssCadre.getSanction());
        after.setStatus(before.getStatus());
        after.setRemarks(sysAssCadre.getRemarks());
        after.setLastupdMan(UserConst.getCurrentUsername(map));
        after.setLastupdTime(new Date());

        sysAssCadreMapper.updateByPrimaryKeySelective(after);
    }

    /**
     * 分页获取社联干部列表
     *
     * @param assId
     * @param keyword
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult<Map> getPageByKeyword(Integer assId, String keyword, PageQuery pageQuery) {
        int count = sysAssCadreMapper.countByAssIdAndKeyword(assId, keyword);
        if (count > 0) {
            List<Map> list = sysAssCadreMapper.getPageByAssIdAndKeyword(assId, keyword, pageQuery);
            PageResult<Map> result = new PageResult<>();
            result.setList(list);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }

    /**
     * 根据id获取社团干部信息
     *
     * @param id
     * @return
     */
    @Override
    public SysAssCadre findById(Integer id) {
        SysAssCadre sysAssCadre = sysAssCadreMapper.selectByPrimaryKey(id);
        if (sysAssCadre == null) {
            throw new ParamException("获取社团干部信息失败");
        }
        return sysAssCadre;
    }

    /**
     * 修改社团干部状态
     *
     * @param ids
     * @param status
     * @param username
     */
    @Override
    public void updateStatus(Integer[] ids, int status, String username) {
        Map params = new HashMap(8);
        params.put("status", status);
        params.put("lastupdTime", new Date());
        params.put("lastupdMan", username);
        sysAssCadreMapper.updateStatusByPrimaryKey(ids, params);
    }

    /**
     * 批量删除社团干部信息
     *
     * @param ids
     */
    @Override
    public void delete(Integer[] ids) {
        sysAssCadreMapper.batchDeleteByPrimaryKey(ids);
    }
}
